//app.js
App({
  globalData: {
    userInfo: null,
    //allUrl:"https://www.ymg3.com",
    allUrl:"http://192.168.0.102:8080",
  },
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
  },
  getUserInfo: function (cb) {//获取用户的唯一 openid
    var that = this;
    if (this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function (rese) {
          // console.log(rese.code)//用户为的唯一动态code
          wx.getUserInfo({
            success: function (res) {
              that.globalData.hasRoot = false;
              that.globalData.noRoot = true;
              wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本地
              // that.globalData.code=rese.code code每次登陆都会变化，所以传到后台让后台生成一个固定的userid，保存在本地，七天有效，用户打开商城时自动验证并登陆。
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
              wx.request({
                url: that.globalData.allUrl + "/YMGW/utilAction/wxLogin",
                data: {
                  code: rese.code,
                  username: res.userInfo.nickName,
                  referrerId:1
                },
                method: 'GET',
                success: function (res) {
                  // success
                  // console.log(res.data)
                  wx.setStorageSync('userid', res.data)
                }
              })
            }, fail: function () {
              that.globalData.hasRoot = true;
              that.globalData.noRoot = false;
            }
          })
        }
      })
    }
  },
})